package weaver.splittable.db.mysql;

import org.apache.commons.lang3.StringUtils;
import weaver.splittable.db.STCreateIndexUtil;
import weaver.splittable.db.STDBConstants;

import java.util.List;

/**
 * 建索引工具的Mysql实现
 * author: YUEYANG 2023-01-12
 */
public class STMysqlCreateIndexUtil implements STCreateIndexUtil {

    private static final String CREATE_CHANGE_INDEX_ID = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_dci_id(id)";
    private static final String CREATE_CHANGE_INDEX_DATAID = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_dci_dataid(data_id)";
    private static final String CREATE_CHANGE_INDEX_SYNCED = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_dci_synced(synced)";
    private static final String CREATE_CHANGE_INDEX_SYNCED_TABLENAME_EVENTTYPE = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_dci_stnet(synced, table_name, event_type)";
    private static final String CREATE_CHANGE_INDEX_SYNCED_SYNCTIME = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_dci_ss(synced, sync_time)";

    private static final String CREATE_CLEAR_INDEX_ID = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_jcdi_synced(id)";
    private static final String CREATE_CLEAR_INDEX_DATAID = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_jcdi_dataid(data_id)";
    private static final String CREATE_CLEAR_INDEX_SYNCED = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_jcdi_synced(synced)";
    private static final String CREATE_CLEAR_INDEX_SYNCED_TABLENAME_DATAID = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_jcdi_synced_tn_di(synced, table_name, data_id)";
    private static final String CREATE_CLEAR_INDEX_SYNCED_SYNCTIME = "ALTER TABLE "+ STDBConstants.DATA_CHANGE_TABLE_NAME +" ADD INDEX idx_jcdi_ss(synced, sync_time)";

    private static final String CREATE_INDEX_SQL = "ALTER TABLE %s ADD INDEX %s (%s)";

    @Override
    public String createChangeIndex_Id() {
        return CREATE_CHANGE_INDEX_ID;
    }

    @Override
    public String createChangeIndex_DataId() {
        return CREATE_CHANGE_INDEX_DATAID;
    }

    @Override
    public String createChangeIndex_Synced() {
        return CREATE_CHANGE_INDEX_SYNCED;
    }

    @Override
    public String createChangeIndex_SyncedTableNameEventType() {
        return CREATE_CHANGE_INDEX_SYNCED_TABLENAME_EVENTTYPE;
    }

    @Override
    public String createChangeIndex_SyncedSyncTime() {
        return CREATE_CHANGE_INDEX_SYNCED_SYNCTIME;
    }

    @Override
    public String createClearIndex_Id() {
        return CREATE_CLEAR_INDEX_ID;
    }

    @Override
    public String createClearIndex_DataId() {
        return CREATE_CLEAR_INDEX_DATAID;
    }

    @Override
    public String createClearIndex_Synced() {
        return CREATE_CLEAR_INDEX_SYNCED;
    }

    @Override
    public String createClearIndex_SyncedTableNameDataId() {
        return CREATE_CLEAR_INDEX_SYNCED_TABLENAME_DATAID;
    }

    @Override
    public String createClearIndex_SyncedSyncTime() {
        return CREATE_CLEAR_INDEX_SYNCED_SYNCTIME;
    }

    @Override
    public String createIndex(String indexName, String tableName, List<String> columnNames) {
        String columns = StringUtils.join(columnNames, ",");
        return String.format(CREATE_INDEX_SQL, tableName, indexName, columns);
    }
}
